// MyFamily - XML based genealogy program.
// Copyright (C) 2006  Doug Swisher
// See the file COPYING.txt for license details.

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;


namespace DataModel
{
    public class FamilyEvent
    {
        private string kind;            // TODO - Doug - 8/4/2006 - turn into an enum...that is expandable...at run time...hmm...
        private string date;            // TODO - Doug - 8/4/2006 - turn into a FamilyDate
        private string place;           // TODO - Doug - 8/4/2006 - turn into a FamilyPlace
        private FamilyExtract extract;



        public FamilyEvent(FamilyExtract extract)
        {
            this.extract = extract;
        }


        public static FamilyEvent LoadFromGedCom(GedComNode node, GedComImportResults results, FamilyExtract extract)
        {
            FamilyEvent eve = new FamilyEvent(extract);

            // TODO - Doug - 8/4/2006 - Set the event type based on the tag of the specified node
            eve.Kind = node.Tag;

            foreach (GedComNode child in node.Children)
            {
                switch (child.Tag)
                {
                    case "ADDR":
                        // TODO - Doug - 8/12/2006 - Figure out what to do with event ADDR tags
                        break;

                    case "FAMC":
                        // TODO - Doug - 8/12/2006 - Figure out what to do with event FAMC tags
                        break;

                    case "CENS":
                        // TODO - Doug - 8/4/2006 - Figure out what to do with event CENS tags
                        break;
                    
                    case "DATE":
                        eve.Date = child.Value;
                        break;

                    case "NOTE":
                        // TODO - Doug - 8/4/2006 - Figure out what to do with event NOTE tags
                        break;

                    case "PLAC":
                        eve.Place = child.Value;
                        break;

                    case "QUAY":
                        // TODO - Doug - 8/4/2006 - Figure out what to do with event SOUR tags
                        break;

                    case "SOUR":
                        // TODO - Doug - 8/4/2006 - Figure out what to do with event SOUR tags
                        break;

                    default:
                        results.LogUnhandledTag(node, child);
                        break;
                }
            }

            return eve;
        }


        public void SaveXml(XmlWriter writer)
        {
            writer.WriteStartElement("event");
            writer.WriteAttributeString("kind", this.kind);
            writer.WriteAttributeString("place", this.place);   // TODO - Doug - 8/27/2006 - this should be an ID once places are implemented
            writer.WriteAttributeString("date", this.date);
            writer.WriteEndElement();
        }


        public void ParseXml(XmlElement eventElement)
        {
            string val = eventElement.GetAttribute("kind");
            if (!string.IsNullOrEmpty(val))
            {
                this.kind = val;
            }

            val = eventElement.GetAttribute("place");
            if (!string.IsNullOrEmpty(val))
            {
                this.place = val;
            }

            val = eventElement.GetAttribute("date");
            if (!string.IsNullOrEmpty(val))
            {
                this.date = val;
            }
        }


        public override bool Equals(object obj)
        {
            // Simple cases...
            if (obj == null)
            {
                return false;
            }

            FamilyEvent other = obj as FamilyEvent;

            if (other == null)
            {
                return false;
            }

            // Check members...
            if (!string.Equals(this.date, other.date))
            {
                return false;
            }

            if (!string.Equals(this.kind, other.kind))
            {
                return false;
            }

            if (!string.Equals(this.place, other.place))
            {
                return false;
            }

            // TODO - 8/27/2006 - Doug - check source/citation/whatever it's going to be called
            // TODO - 8/27/2006 - Doug - add unit tests for equality

            return true;
        }


        public override int GetHashCode()
        {
            // TODO - Doug - 8/27/2006 - do we want to change this implementation at all?
            return base.GetHashCode();
        }


        public override string ToString()
        {
            // TODO - Doug - 8/27/2006 - implement a better version of ToString()
            return this.kind;
        }


        public string Date
        {
            get { return this.date; }
            set { this.date = value; }
        }

        public string Place
        {
            get { return this.place; }
            set { this.place = value; }
        }

        public string Kind
        {
            get { return this.kind; }
            set { this.kind = value; }
        }

        // TODO - Doug - 9/3/2006 - get rid of this, and use Extract instead
        public FamilySource Source
        {
            get
            {
                if (this.extract != null)
                {
                    return this.extract.Source;
                }
                else
                {
                    return null;
                }
            }
        }
    }
}
